package com.wys.ws_im.controller;

import com.wys.ws_im.entity.Account;
import com.wys.ws_im.entity.Shop;
import com.wys.ws_im.map.OnlineMap;
import com.wys.ws_im.service.UserService;
import com.wys.ws_im.utils.ResultUtil;
import com.wys.ws_im.vo.ResultVO;
import com.wys.ws_im.vo.TokenVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 用户信息模块
 *
 *
 * @author wys
 * created in 12:34 2019/4/9
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private LoginState loginState;

    /**
     * 登录
     *
     * @return
     */
    @PostMapping("/login")
    public ResultVO login(@RequestParam String account,
                          @RequestParam String password) {

        if (!OnlineMap.onlineUserMap.containsKey(account)) {

            TokenVO tokenVO = userService.login(account, password);
            OnlineMap.onlineUserMap.put(account, tokenVO);

            return ResultUtil.success(0, "登录成功", tokenVO);
        }
        TokenVO onlineTokenVO = OnlineMap.onlineUserMap.get(account);

        return ResultUtil.success(0, "已经登录", onlineTokenVO);
    }

    /**
     * 注册
     * @param account   账号
     * @return
     */
    @PostMapping("/register")
    public ResultVO register(@Valid Account account) {
        userService.register(account);
        return ResultUtil.success();
    }

    /**
     * shop 增加商店
     * @param shop   shop
     * @return
     */
    @PostMapping("/shop")
    public ResultVO shop(@Valid Shop shop) {
        userService.shop(shop);
        return ResultUtil.success();
    }

    /**
     * 退出
     * @param account
     * @return
     */
    @PostMapping("/logout")
    public ResultVO exit(@RequestParam String account) {

        Boolean logout = userService.logout(account);
        if (logout) {
            if (!OnlineMap.onlineUserMap.containsKey(account)) {
                return ResultUtil.success(100, "还没有登录");
            }
        }
        OnlineMap.onlineUserMap.remove(account);
        return ResultUtil.success(0, "退出成功");
    }

    /**
     * 修改用户个人信息
     * @param address
     * @return
     */
    @GetMapping("/upUserinfo")
    public ResultVO updateInfo(Integer aid, String address) {
        userService.updateInfo(aid, address);
        return ResultUtil.success("修改用户个人信息成功");
    }

    /**
     * 修改客服个人信息
     * @param phone
     * @return
     */
    @GetMapping("/upServiceinfo")
    public ResultVO updateServiceInfo(Integer aid, String phone) {
        userService.updateServiceInfo(aid, phone);
        return ResultUtil.success("修改客服个人信息成功");
    }

    /**
     * 店主增加客服接口
     * @param shopId
     * @return
     */
    @PostMapping("/addService")
    public ResultVO addService(Integer shopId, String serviceName) {
        userService.addService(shopId, serviceName);
        return ResultUtil.success("增加成功");
    }

    /**
     * 店主移除客服接口
     * @param serviceName
     * @return
     */
    @PostMapping("/removeService")
    public ResultVO removeService(Integer shopId, String serviceName) {
        userService.removeService(shopId, serviceName);
        return ResultUtil.success("移除客服成功");
    }

    /**
     * 获得在线状态
     * @param receviId
     * @return
     */
    @GetMapping("/getStatus")
    public ResultVO getStatus(Integer receviId) {

        String status = userService.getStatus(receviId);
        return ResultUtil.success(status);

    }

    /**
     * 获得账号
     */
    @GetMapping("getAccount")
    public ResultVO getAccount() {

        List<Account> accounts = userService.getAccount();

        return ResultUtil.success(accounts);

    }

    /**
     * 获得账号
     */
    @GetMapping("getShop")
    public ResultVO getShop() {

        List<Shop> shops = userService.getShop();

        return ResultUtil.success(shops);

    }

    /**
     * 获得商店名字
     */
    @GetMapping("getShopName")
    public ResultVO getShopName(String serviceName) {

        String shopName = userService.getShopName(serviceName);

        return ResultUtil.success(shopName);

    }

}
